package com.sftz.modules.minapp.homework.dto;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.sftz.assets.entities.question.HomeworkFileQuestionEntity;
import com.sftz.assets.entities.question.HomeworkQuestionAnswerFileEntity;
import com.sftz.assets.enums.QuestionTypeEnum;
import com.sftz.assets.utils.QuestionUtil;
import com.sftz.framework.util.CompareUtil;
import com.sftz.framework.util.WeappHtmlUtil;
import com.sftz.modules.common.dto.FillCountDto;
import com.sftz.modules.common.dto.FillInStemDto;
import com.uxuexi.core.common.util.Util;

/**
 * 作业的试题dto
 * 
 * @author   朱晓川
 * @Date	 2018年12月23日
 */
@Data
public class HomeworkAnswerQuestionDto implements Comparable<HomeworkAnswerQuestionDto> {

	//试题id
	private Integer id;

	//题型
	private Integer questionType;

	//题型名称
	private String questionTypeName;

	//所属模块名称
	private String moduleName;

	//题干
	private String stem;

	//主客观类型
	private Integer objective;

	//答案选项(id)
	private String answerOption;

	//答案
	private String answer;

	//试题解析
	private String analysis;

	/**
	 * 用户答案的id
	 * <p>
	 * 可能为空，因为答题的时候，此次作答对于该题也许还没保存用户的答案
	 */
	private Integer answerId;

	//用户答案的内容
	private String answerContent;

	//用户的答案-label
	private String answerContentLabel;

	//用户的答案-集合
	private List<String> answerList = new ArrayList<>();

	//模块序号
	private Integer msort;

	//试题序号
	private Integer rsort;

	//试题的分数
	private Integer score;

	//得分
	private int actualScore;

	//材料限制上传个数
	private Integer limitedNumber;

	//材料限制上传大小
	private Integer limitSize;

	//答案是否已保存
	private boolean saved;

	//回答是否正确(1正确2错误)
	private boolean answerResult;

	//试题的作答结果是否改变
	private boolean resultChanged = false;

	//试题的选项(客观题会有)
	private List<HomeworkQuestionOptionDto> optLst;

	/**
	 * 填空题的空
	 */
	private List<FillCountDto> fillList;

	//材料题的文件
	private List<HomeworkFileQuestionEntity> questFiles;

	//材料题的答案文件
	private List<HomeworkQuestionAnswerFileEntity> answerFiles;

	/**
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		HomeworkAnswerQuestionDto other = (HomeworkAnswerQuestionDto) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	/**
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public int compareTo(HomeworkAnswerQuestionDto that) {
		int re = CompareUtil.safeCompareToGt(this.msort, that.msort);
		if (0 == re) {
			//比较rsort
			return CompareUtil.safeCompareToGt(this.rsort, that.rsort);
		}
		return re;
	}

	//设置选项是否选中-作答页
	public void setOptActive() {
		if (!Util.isEmpty(answerContent) && !Util.isEmpty(optLst)) {
			List<String> sp = Splitter.on(",").splitToList(answerContent);
			for (int i = 0; i < optLst.size(); i++) {
				HomeworkQuestionOptionDto op = optLst.get(i);
				String optId = op.getOptId() + "";
				if (null != sp && sp.contains(optId)) {
					op.setActive("active");
				}
			}
		}
	}

	//设置试题答案详情-作答详情页使用
	public void setQuestAnswer() {
		stem = WeappHtmlUtil.clearHtml(stem, "p");

		//客观题
		if (!Util.isEmpty(answerContent) && !Util.isEmpty(optLst)) {
			List<String> sp = Splitter.on(",").splitToList(answerContent);
			for (int i = 0; i < optLst.size(); i++) {
				HomeworkQuestionOptionDto op = optLst.get(i);
				String optId = op.getOptId() + "";
				if (null != sp && sp.contains(optId)) {
					op.setActive("active");
					String opt = op.getOpt();
					answerList.add(opt);
				}
			}
			if (!Util.isEmpty(answerList)) {
				this.answerContentLabel = Joiner.on(",").join(answerList);
			}

			//得分
			if (QuestionUtil.objectiveCorrect(answerOption, answerContent)) {
				this.actualScore = this.score;
				//回答正确
				this.answerResult = true;
			} else {
				this.answerResult = false;
				this.actualScore = 0;
			}
		}

		//填空题
		if (!Util.isEmpty(questionType) && QuestionTypeEnum.FILLIN.intKey() == questionType) {
			FillInStemDto fd = QuestionUtil.fillInStemReplace(stem, answerContent);
			stem = fd.getStem();
			fillList = fd.getFillList();

			answer = QuestionUtil.restoreFillinAnswer(answer);
			//此步应该在设置了fillList之后调用
			answerContent = QuestionUtil.restoreFillinAnswerContent(fillList);
		}
	}

}
